package com.example.toutiao.ui.main.adpater

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.toutiao.R
import com.example.toutiao.ui.main.model.CommonModel

class CommonAdapter(mCommonList:MutableList<CommonModel>):RecyclerView.Adapter<CommonAdapter.CommonHolder>() {
    private val mCommonList:MutableList<CommonModel>?=mCommonList

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommonHolder=
        /**
         * 加载需要的布局
         */
        CommonHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_common_view,parent,false))

    override fun onBindViewHolder(holder: CommonHolder, position: Int) {
        if(mCommonList!=null){
            val mCommonModel=mCommonList[position]
            holder.mTitle.text=mCommonModel.title
            if(mCommonModel.list!=null){
                /* 关联对应的适配器*/
                holder.mCommonList.layoutManager= GridLayoutManager(holder.itemView.context,4)
                holder.mCommonList.adapter=CommonItemAdapter(mCommonModel.list)
            }

        }
    }

    override fun getItemCount(): Int =mCommonList!!.size

    /**
     * 作用是拿到view
     */
    class CommonHolder(it: View):RecyclerView.ViewHolder(it){
        /*通过适配器 在通过id 拿到对应的视图组件*/
        val mTitle:TextView=it.findViewById(R.id.tv_title)
        val mCommonList:RecyclerView=it.findViewById(R.id.rv_common_list)
    }
}